package cn.cdiothree.service.impl;

import cn.cdiothree.dao.ActivityApplyMapper;
import cn.cdiothree.dao.ActivityInfoMapper;
import cn.cdiothree.dataobject.ActivityApply;
import cn.cdiothree.dataobject.ActivityInfo;
import cn.cdiothree.enums.MessageEnum;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.exception.UserException;
import cn.cdiothree.service.ActivityApplyService;
import cn.cdiothree.vo.ActivityApplyVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动报名实现类
 */
@Service
@Slf4j
public class ActivityApplyServiceImpl extends ServiceImpl<ActivityApplyMapper, ActivityApply> implements ActivityApplyService {
    @Autowired
    private ActivityApplyMapper activityApplyMapper;
    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    /**
     * 申请活动
     * @param activityApply
     * @return
     */
    @Override
    public ActivityApply create(ActivityApply activityApply){
        ActivityApply activityApply1 = new ActivityApply();
        activityApply1.setApplyId(activityApply.getApplyId());
        activityApply1.setActivityId(activityApply.getActivityId());
        activityApply1.setUserId(activityApply.getUserId());
        activityApply1.setStudentNumber(activityApply.getStudentNumber());
        activityApply1.setStudentName(activityApply.getStudentName());
        activityApply1.setStudentClass(activityApply.getStudentClass());
        activityApply1.setTelephone(activityApply.getTelephone());
        activityApply1.setCreateTime(activityApply.getCreateTime());
        activityApply1.setUpdateTime(activityApply.getUpdateTime());
        log.info("用户{}进行活动{}申请操作",activityApply.getUserId(),activityApply.getActivityId());
        //判断是否已经报名此活动
        QueryWrapper<ActivityApply> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",activityApply.getUserId());
        List<ActivityApply> activityApplyList = activityApplyMapper.selectList(wrapper);
        for (ActivityApply activityApply2:activityApplyList){
            if (activityApply2.getActivityId().equals(activityApply.getActivityId())){
                throw new UserException(MessageEnum.APPLY_HAS_EXIST);
            }
        }
        int insert = activityApplyMapper.insert(activityApply1);
        if (insert<1){
            throw new SellException(ResultEnum.ADD_APPLY_FAILED);
        }
        return activityApply1;


    }

    /**
     * 根据用户id查找活动列表
     * @param userId
     * @return
     */
    @Override
    public List<ActivityApplyVO> findList(Integer userId){
        log.info("用户id{}",userId);
        QueryWrapper<ActivityApply> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ActivityApply> activityApplyList = activityApplyMapper.selectList(wrapper);
        List<ActivityApplyVO> activityApplyVOList = new ArrayList<>();
        for (ActivityApply activityApply:activityApplyList){
            QueryWrapper<ActivityInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("activity_id",activityApply.getActivityId());
            ActivityInfo activityInfo = activityInfoMapper.selectOne(wrapper1);
            if (activityInfo==null){
                continue;
            }
            ActivityApplyVO activityApplyVO = new ActivityApplyVO();
            activityApplyVO.setActivityId(activityInfo.getActivityId());
            activityApplyVO.setActivityName(activityInfo.getActivityName());
            activityApplyVO.setActivityPost(activityInfo.getActivityPost());
            activityApplyVO.setStudentNumber(activityApply.getStudentNumber());
            activityApplyVO.setStudentName(activityApply.getStudentName());
            activityApplyVO.setStudentClass(activityApply.getStudentClass());
            activityApplyVO.setTelephone(activityApply.getTelephone());
            activityApplyVOList.add(activityApplyVO);
        }
        return activityApplyVOList;
    }
}
